package com.cluster.gulimall.order.app;


import com.cluster.gulimall.order.entity.OmsOrderReturnReason;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dqh
 * @since 2024-08-20
 */
@RestController
@RequestMapping("/mq-message")
@Slf4j
public class MqMessageController {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/currentUserCartItems")
    @ResponseBody
    public void createExchange(){
        DirectExchange directExchange = new DirectExchange(
                "hello-java-exchange",
                true,
                false);
        amqpAdmin.declareExchange(directExchange);
        log.info("exchange{}创建成功",directExchange.getName());
    }

    @GetMapping(value = "/sendEntity")
    @ResponseBody
    public void sendEntity(){
        OmsOrderReturnReason entity = new OmsOrderReturnReason();
        entity.setId(1L);
        entity.setCreateTime(LocalDateTime.now()/*.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))*/);
        entity.setName("哈哈");

        rabbitTemplate.convertAndSend(
                "hello-java-exchange",
                "hello.java",
                entity
        );
        log.info("消息发送完成：{}",entity);
    }
}
